<?php
	include_once(dirname(__DIR__) . "/include/dbconnection.php");
	include_once(dirname(__DIR__) . "/include/util.php");

	class Forum {
		private $forum_id, $upper_forum_id, $name, $description, $closed, $no_threads;
		private $forums = array(), $threads = array();
		
		private $html;
		
		function Forum($forum_id) {
			$this -> loadForumFromDatabase($forum_id);
			$this -> loadForumsFromDatabase($forum_id);
			$this -> loadThreadsFromDatabase($forum_id);
			$this -> generateForumHTML();
			$this -> generateNewThreadForm();
		}
		
		private function loadForumFromDatabase($forum_id) {
			$query = "SELECT forum_id, upper_forum_id, name, description, closed, no_threads FROM t_forum WHERE forum_id = " . intval($forum_id);
			$result = mysql_query($query);
			$row = mysql_fetch_assoc($result);

			$this -> forum_id = $row['forum_id'];
			$this -> upper_forum_id = $row['upper_forum_id'];
			$this -> name = $row['name'];
			$this -> description = $row['description'];
			$this -> closed = $row['closed'];
			$this -> no_threads = $row['no_threads'];

			mysql_free_result($result);
		}
		
		private function loadForumsFromDatabase($upper_forum_id) {
			$query = "SELECT forum_id, upper_forum_id, name, description, closed FROM t_forum WHERE upper_forum_id = " . intval($upper_forum_id);
			$result = mysql_query($query);
			$row_count = mysql_num_rows($result);
			
			for($row_counter = 0; $row_counter < $row_count; $row_counter++) {
				$row = mysql_fetch_assoc($result);
				$this -> forums[] = $row;
			}
			
			mysql_free_result($result);
		}
		
		private function loadThreadsFromDatabase($forum_id) {
			$query = "SELECT thread_id, name, closed, last_activity, creation FROM t_thread WHERE forum_id = " . intval($forum_id);
			$result = mysql_query($query);
			$row_count = mysql_num_rows($result);
			
			for($row_counter = 0; $row_counter < $row_count; $row_counter++) {
				$row = mysql_fetch_assoc($result);
				$this -> threads[] = $row;
			}
			
			mysql_free_result($result);
		}
		
		//Generate HTML
		
		private function generateForumHTML() {
			$this -> html =
				"<div id=\"forum\">".
					"<h2>Forum: ". $this -> name ."</h2>";
			
			foreach($this -> forums as $forum) {
				$this -> html .=
					"<div class=\"sub_forum\">".
						"<i><a href=\"./index.php?forum_id=". $forum['forum_id'] ."\">". $forum['name'] ."</a></i>".
					"</div>";
			}
			
			foreach($this -> threads as $thread) {
				if(!$thread['closed']) {
					$this -> html .=
						"<div class=\"thread\">".
							"<b><a href=\"./index.php?thread_id=". $thread['thread_id'] ."\">". $thread['name'] ."</a></b><br />".
							"&nbsp;&nbsp;<a href=\"". getUrl() ."&close=". $thread['thread_id'] ."\">schliessen</a>".
						"</div>";
				}
			}
			
			$this -> html .=
				"</div>";
		}
		
		private function generateNewThreadForm() {
			$this -> html .=
				"<div id=\"threadform\">".
					"<h2>Neuen Thread erstellen:</h2>".
					"<form action=\"".getUrl()."&new"."\" method=\"post\" enctype=\"multipart/form-data\">".
						"<p>Threadname:<input name=\"name\" type=\"text\" size=\"52\" maxlength=\"50\"></p>".
						"<p>Content:   <textarea name=\"content\" cols=\"52\" rows=\"10\"></textarea></p>".
						"<p><input type=\"submit\" value=\"submit\"> <input type=\"reset\" value=\"reset\"></p>".
					"</form>".
				"</div>";
		}
		
		public function show() {
			echo $this -> html;
		}
	}
?>